package gvo.sql;

import gvo.dbinfo.SarFieldInfo;
import gvo.dbinfo.TableInfo;
import gvo.util.GVOstore;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;

import org.hsqldb.util.TableSorter;




public class ShowResults extends JFrame implements WindowListener {
	private JTable resultate;
	private static int MAXCOL = 20;
	private String[][] tableDaten;
	private String[] tableKopf;
	private ResultSet rs;
	private Statement st;
	private int rowcount;
	

	public ShowResults(String pQuery, GVOstore gvostore) throws SQLException {
		setTitle("Ergebnisse");
		setSize(600, 400);
		//System.out.println(">>" + pQuery);
		st = null;
		st = gvostore.sqlTool.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		
		rs = st.executeQuery(pQuery);
		ResultSetMetaData infoRS = rs.getMetaData();
		int colcount = infoRS.getColumnCount();
		if (colcount > MAXCOL)
			colcount = MAXCOL;
		rowcount = 0;
		while (rs.next()) {
			rowcount++;
		}
		if (rowcount == 0) {
			System.out.println("Leeres ResultSet!");
			JLabel nachricht = new JLabel("Keine Daten vorhanden!", SwingConstants.CENTER);
			Font bFont = new Font("Tahoma", Font.PLAIN, 16);
			nachricht.setFont(bFont);
			getContentPane().add(nachricht, BorderLayout.CENTER);
		} else {
			tableDaten = new String[rowcount][colcount];
			tableKopf = new String[colcount];
			rs.first();
			for (int i = 1; i <= colcount && i <= MAXCOL; i++) {
				tableKopf[i - 1] = infoRS.getColumnName(i);
				System.out.println(tableKopf[i - 1]);
			}
			int zaehlerRow = 0;
			do {
				for (int i = 1; i <= colcount && i <= MAXCOL; i++) {
					tableDaten[zaehlerRow][i - 1] = rs.getString(i);
				}
				zaehlerRow++;
			} while (rs.next());
			
			TableSorter sorter = new TableSorter(new MyTableModel());
			resultate = new JTable(sorter);
			sorter.setTableHeader(resultate.getTableHeader());
			JScrollPane scrollPane = new JScrollPane(resultate);
			getContentPane().add(scrollPane, BorderLayout.CENTER);
			
		}
		setVisible(true);
	}
	public ShowResults(TableInfo ti, GVOstore gvostore) throws SQLException {
		setTitle("Tabelle " + ti.tableName);
		setSize(600, 400);
		String pQuery = "Select * from " + ti.tableName;
		//System.out.println(">>" + pQuery);
		st = null;
		st = gvostore.sqlTool.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		rs = st.executeQuery(pQuery);
		//ResultSetMetaData infoRS = rs.getMetaData();
		int colcount = ti.fields.size();
		rowcount = 0;
		while (rs.next()) {
			rowcount++;
		}
		if (rowcount == 0) {
			System.out.println("Leeres ResultSet!");
			JLabel nachricht = new JLabel("Keine Daten vorhanden!", SwingConstants.CENTER);
			Font bFont = new Font("Tahoma", Font.PLAIN, 16);
			nachricht.setFont(bFont);
			getContentPane().add(nachricht, BorderLayout.CENTER);
		} else {
			tableDaten = new String[rowcount][colcount];
			tableKopf = new String[colcount];
			rs.first();
			int nr=0;
			for (SarFieldInfo fi : ti.fields) {
				tableKopf[nr++] = fi.toString();
			}
			
			int zaehlerRow = 0;
			do {
				for (int i = 1; i <= colcount && i <= MAXCOL; i++) {
					tableDaten[zaehlerRow][i - 1] = rs.getString(i);
				}
				zaehlerRow++;
			} while (rs.next());
			
			TableSorter sorter = new TableSorter(new MyTableModel());
			resultate = new JTable(sorter);
			sorter.setTableHeader(resultate.getTableHeader());
			JScrollPane scrollPane = new JScrollPane(resultate);
			getContentPane().add(scrollPane, BorderLayout.CENTER);
		}
		setVisible(true);
	}
	// WindowListener
	public void windowOpened(WindowEvent e) {
		//nix
	}

	public void windowClosing(WindowEvent e) {
		try {
			if (st != null)
				st.close();
		} catch (SQLException s_ex) {
			s_ex.printStackTrace();
		}
	}

	public void windowClosed(WindowEvent e) {
		//nix
	}

	public void windowIconified(WindowEvent e) {
		//nix
	}

	public void windowDeiconified(WindowEvent e) {
		//nix
	}

	public void windowActivated(WindowEvent e) {
		//nix
	}

	public void windowDeactivated(WindowEvent e) {
		//nix
	}

	private class MyTableModel extends AbstractTableModel {
		public int getColumnCount() {
			return tableKopf.length;
		}

		public int getRowCount() {
			return tableDaten.length;
		}

		public String getColumnName(int col) {
			return tableKopf[col];
		}

		public Object getValueAt(int row, int col) {
			return tableDaten[row][col];
		}

		public Class<?> getColumnClass(int c) {
			try{
				return getValueAt(0, c).getClass();
			}
			catch(NullPointerException x){
				try {
					return Class.forName("java.lang.String");
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
					return null;
				}
			}
		}
	}
	
	
}
